package com.safety.web.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.safety.core.util.Data;
import com.safety.web.entity.Healthheadlines;
import com.safety.web.entity.Theheadlines;
import com.safety.web.service.HealthheadlinesService;
import com.safety.web.service.TheheadlinesService;




@Controller
@RequestMapping("/healthheadlines")
public class HealthheadlinesController {
	@Resource
	private HealthheadlinesService healthheadlinesService;
	@Resource
	private TheheadlinesService theheadlinesService;

	@RequestMapping("/selectconditionHealthheadlines")
	@ResponseBody
	public Map<String, Object> selectconditionHealthheadlines(int id) {
		if(id==1){
			Map<String, Object> result = new LinkedHashMap<>();
			List<Theheadlines> list1 = theheadlinesService.selectTheheadlines();
			List<Map<String, Object>> message = new ArrayList<>();
			if (list1.size() > 0) {
				for (Theheadlines classList1 : list1) {
					Map<String, Object> listForAreatitle = new LinkedHashMap<String, Object>();
					List<Map<String, Object>> message1 = new ArrayList<>();
					listForAreatitle.put("id", classList1.getId());
					listForAreatitle.put("heatitle", classList1.getName());
					String[] littlePic = classList1.getImgurl().split(",");// 分割出来的字符数组
					for (int i = 0; i < littlePic.length; i++) {
						Map<String, Object> map1 = new LinkedHashMap<String, Object>();
						map1.put("liimg",Data.URL.base + littlePic[i]);
						message1.add(map1);
					}
					listForAreatitle.put("img", message1);

					message.add(listForAreatitle);
				}
			}
			if (list1.size() > 0) {
				result.put("theheadlines", message); 
				result.put("code", 200);
			} else{
				result.put("code", 400);
			}

			
			return result;
		}else{
			Map<String, Object> result = new LinkedHashMap<>();
			List<Theheadlines> list1 = theheadlinesService.selectconditionHealthheadlines(id);
			List<Map<String, Object>> message = new ArrayList<>();
			if (list1.size() > 0) {
				for (Theheadlines classList1 : list1) {
					Map<String, Object> listForAreatitle = new LinkedHashMap<String, Object>();
					List<Map<String, Object>> message1 = new ArrayList<>();
					listForAreatitle.put("id", classList1.getId());
					listForAreatitle.put("heatitle", classList1.getName());
					String[] littlePic = classList1.getImgurl().split(",");// 分割出来的字符数组
					for (int i = 0; i < littlePic.length; i++) {
						Map<String, Object> map1 = new LinkedHashMap<String, Object>();
						map1.put("liimg",Data.URL.base + littlePic[i]);
						message1.add(map1);
					}
					listForAreatitle.put("img", message1);

					message.add(listForAreatitle);
				}

			}
			if (list1.size() > 0) {
				result.put("theheadlines", message); 
				result.put("code", 200);
			} else{
				result.put("code", 400);
			}
			return result;
		}
		
	}
	@RequestMapping("/selectHealthheadlinestitle")
	@ResponseBody
	public Map<String, Object> selectHealthheadlinestitle() {
		Map<String, Object> result = new LinkedHashMap<>();
		List<Healthheadlines> list = healthheadlinesService.selectHealthheadlines();
		List<Map<String, Object>> message = new ArrayList<>();
		for (Healthheadlines classList : list) {
			Map<String, Object> map2= new LinkedHashMap<String, Object>();
			map2.put("id", classList.getId());
			map2.put("heatitle", classList.getTitle());
			message.add(map2);
		} 

		result.put("healthheadlines", message);
		result.put("code", 200);
		return result;
		
	}
}
